package cn.itsource.hrm.util;

import cn.itsource.hrm.exception.GlobalException;
import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertUtils {

    //不可为空
    static public void isnotNull(String message, Object obj) {
        if(obj == null){
            throw new GlobalException(message);
        }

        if(obj instanceof  String){
            String str = (String)obj;
            if(StringUtils.isEmpty(str)){
                throw new GlobalException(message);
            }
        }
    }



    //正则的校验 合法性
    static public void isRegIlegal (String message, String regex , String value ) {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(value);

            if(!m.matches()){
                throw new GlobalException(message);
            }

    }

    //营业执照的合法
    static public void isBusinessLicense(String message,String value ){
        if(!ValidatorUtils.isLicense18(value)){
            throw new GlobalException(message);
        }
    }

    // 断言为真
   static public void isTrue(String message,Boolean bool){
        if(!bool){
            throw new GlobalException(message);
        }
   }

    /**
     * 相同会抛异常
     * @param message
     * @param obj1
     * @param obj2
     */
   static  public void isEquals(String message,Object obj1,Object obj2){
       isnotNull("对象不能为空",obj1);
       isnotNull("对象不能为空",obj2);

        if(obj1.equals(obj2)){
            throw new GlobalException(message);
        };
   }

    static  public void isNotEquals(String message,String obj1,String obj2){
        isnotNull("对象不能为空",obj1);
        isnotNull("对象不能为空",obj2);

        if(!obj1.equals(obj2)){
            throw new GlobalException(message);
        };
    }
}
